<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\Content;
use App\Service\AesService;
use App\Service\GameApiService;
use Co\Redis;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Annotation\AutoController;
use function Hyperf\ViewEngine\view;

/**
 * @AutoController
 * Class TestController
 */
class TestController extends AbstractController
{

    /**
     * @Inject
     * @var ClientFactory
     */
    protected $clientFactory;


    /**
     * @Inject
     * @var AesService
     */
    protected $aesService;

    public function test()
    {
        $obj = $this->container->get(GameApiService::class);
        return $obj->CYJL('一马当先');
    }

    public function upload()
    {
        $contents = Content::orderByDesc('id')->get();
        return view('up', ['contents' => $contents]);
    }

    public function fileUp()
    {
        $content = $this->request->input('content');
        $data = ['content' => $content, 'created_at' => date('Y-m-d H:i:s')];
        $file = $this->request->file('file');
        if ($file->isValid()) {
            $path = 'storage/'.date('Y-m-dH:i:s-').$file->getClientFilename();
            $file->moveTo($path);
            $data['file'] = $path;
            chmod(BASE_PATH.'/'.$path, 775);
        }
        Content::insert($data);
        return $this->response->redirect('/test/upload');
    }

    public function downloadFile()
    {
        new Redis();
        $file = BASE_PATH.'/'.$this->request->input('file');
        return $this->response->download($file);
    }
}
